package cn.edu.scau.cmi.ema.service;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Crawlerfile;
import cn.edu.scau.cmi.ema.domain.Crawlerfilestatus;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;
@Service("CrawlerfilestatusService")
@Transactional

public class CrawlerfilestatusServiceImpl extends CmiEmaService implements CrawlerfilestatusService {
	public CrawlerfilestatusServiceImpl() {
	}

	@Transactional
	public Crawlerfilestatus saveCrawlerfilestatus(Crawlerfilestatus crawlerfilestatus) {
		Set<Crawlerfile> existingCrawlerfilesForCrawlerfilestatus = new HashSet<Crawlerfile>();

		Set<Crawlerfile> selectedCrawlerfilesForCrawlerfilestatus = crawlerfilestatus
				.getCrawlerfilesForCrawlerfilestatus();

		if (crawlerfilestatus.getId() == null) {

			Crawlerfilestatus existingCrawlerfilestatus = crawlerfilestatusDAO
					.findCrawlerfilestatusByPhysicalPrimaryKey(crawlerfilestatus.getName());

			if (isEquals(crawlerfilestatus, existingCrawlerfilestatus)) {
				return existingCrawlerfilestatus;
			}

			crawlerfilestatus = crawlerfilestatusDAO.store(crawlerfilestatus);
			crawlerfilestatusDAO.flush();

			for (Crawlerfile relatedCrawlerfile : selectedCrawlerfilesForCrawlerfilestatus) {
				relatedCrawlerfile.setCrawlerfilestatus(crawlerfilestatus);
				crawlerfileDAO.store(relatedCrawlerfile);
			}
		} else {
			Crawlerfilestatus existingCrawlerfilestatus = crawlerfilestatusDAO
					.findCrawlerfilestatusByPrimaryKey(crawlerfilestatus.getId());

			existingCrawlerfilesForCrawlerfilestatus = existingCrawlerfilestatus.getCrawlerfilesForCrawlerfilestatus();

			Set<Crawlerfile> prepareDeleteCrawlerfilesForCrawlerfilestatus = new HashSet<Crawlerfile>();
			Set<Crawlerfile> prepareAddCrawlerfilesForCrawlerfilestatus = new HashSet<Crawlerfile>();

			prepareDeleteCrawlerfilesForCrawlerfilestatus.addAll(selectedCrawlerfilesForCrawlerfilestatus);
			prepareDeleteCrawlerfilesForCrawlerfilestatus.addAll(existingCrawlerfilesForCrawlerfilestatus);

			prepareAddCrawlerfilesForCrawlerfilestatus.addAll(selectedCrawlerfilesForCrawlerfilestatus);
			prepareAddCrawlerfilesForCrawlerfilestatus.addAll(existingCrawlerfilesForCrawlerfilestatus);

			prepareDeleteCrawlerfilesForCrawlerfilestatus.removeAll(selectedCrawlerfilesForCrawlerfilestatus);
			prepareAddCrawlerfilesForCrawlerfilestatus.removeAll(existingCrawlerfilesForCrawlerfilestatus);

			for (Crawlerfile relatedCrawlerfile : prepareAddCrawlerfilesForCrawlerfilestatus) {
				relatedCrawlerfile.setCrawlerfilestatus(crawlerfilestatus);
				crawlerfileDAO.store(relatedCrawlerfile);
			}

			for (Crawlerfile relatedCrawlerfile : prepareDeleteCrawlerfilesForCrawlerfilestatus) {
				relatedCrawlerfile.setCrawlerfilestatus(null);
				crawlerfileDAO.store(relatedCrawlerfile);
			}
			crawlerfilestatus = crawlerfilestatusDAO.store(crawlerfilestatus);
			crawlerfilestatusDAO.flush();
		}

		crawlerfileDAO.flush();
		return crawlerfilestatus;
	}

	public void deleteCrawlerfilestatus(Crawlerfilestatus crawlerfilestatus) {
		crawlerfilestatusDAO.remove(crawlerfilestatus);
		crawlerfilestatusDAO.flush();
	}

	@Transactional
	public void deleteCrawlerfilestatuss(Set<Integer> deleteCrawlerfilestatussId) {
		Query query = crawlerfilestatusDAO.createNamedQuery("deleteCrawlerfilestatussById", -1, -1);
		query.setParameter(1, deleteCrawlerfilestatussId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Crawlerfilestatus> loadCrawlerfilestatuss() {
		return crawlerfilestatusDAO.findAllCrawlerfilestatuss();

	}

	@Transactional
	public Set<Crawlerfilestatus> loadReferenceCrawlerfilestatuss(Set<Crawlerfilestatus> crawlerfilestatuss) {
		Iterator<Crawlerfilestatus> crawlerfilestatussIterator = crawlerfilestatuss.iterator();
		Set<Crawlerfilestatus> referencedCrawlerfilestatuss = new HashSet<>();
		while (crawlerfilestatussIterator.hasNext()) {
			Crawlerfilestatus currentCrawlerfilestatus = crawlerfilestatussIterator.next();

			if (!currentCrawlerfilestatus.getCrawlerfilesForCrawlerfilestatus().isEmpty()) {
				referencedCrawlerfilestatuss.add(currentCrawlerfilestatus);
				continue;
			}
		}
		return referencedCrawlerfilestatuss;
	}

	public Set<Crawlerfilestatus> loadCrawlerfilestatussByIdSet(Set<Integer> crawlerfilestatusIdSet) {

		Iterator<Integer> crawlerfilestatusIdIterator = crawlerfilestatusIdSet.iterator();
		Set<Crawlerfilestatus> crawlerfilestatusSet = new HashSet<>();
		while (crawlerfilestatusIdIterator.hasNext()) {
			Integer id = crawlerfilestatusIdIterator.next();
			Crawlerfilestatus currentCrawlerfilestatus = crawlerfilestatusDAO.findCrawlerfilestatusById(id);
			crawlerfilestatusSet.add(currentCrawlerfilestatus);
		}
		return crawlerfilestatusSet;
	}

	@Transactional
	public List<Crawlerfilestatus> findAllCrawlerfilestatuss(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Crawlerfilestatus>(
				crawlerfilestatusDAO.findAllCrawlerfilestatuss(startResult, maxRows));
	}

	@Transactional
	public Crawlerfilestatus findCrawlerfilestatusByPrimaryKey(Integer id) {
		return crawlerfilestatusDAO.findCrawlerfilestatusByPrimaryKey(id);
	}

	@Transactional
	public Integer countCrawlerfilestatuss() {
		return ((Long) crawlerfilestatusDAO.createQuerySingleResult("select count(o) from Crawlerfilestatus o")
				.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerfilestatuss(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Crawlerfilestatus.class);
		return ((Long) crawlerfilestatusDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 *
	 */
	@Transactional
	public Integer countCrawlerfilestatussByName(String queryName) {
		Query query = crawlerfilestatusDAO.createNamedQuery("countCrawlerfilestatussByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countCrawlerfilestatussByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfilestatus.class);
		Session session = (Session) entityManager.getDelegate();
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadCrawlerfilestatusObjectReferencedSet(Crawlerfilestatus crawlerfilestatus) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		Set<Integer> crawlerfilesIds = new HashSet<Integer>();
		for (Crawlerfile obj : crawlerfilestatus.getCrawlerfilesForCrawlerfilestatus()) {

			crawlerfilesIds.add(obj.getId());
		}
		objectReferencedSetMap.put("crawlerfilesIds", crawlerfilesIds);

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadCrawlerfilestatusClassReferencedSet(Crawlerfilestatus crawlerfilestatus) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();
		classReferencedSetMap.put("crawlerfiles", crawlerfileDAO.findAllCrawlerfiles());

		return classReferencedSetMap;
	}

	public boolean isEquals(Crawlerfilestatus crawlerfilestatus, Object obj) {
		if (obj == crawlerfilestatus)
			return true;
		if (!(obj instanceof Crawlerfilestatus))
			return false;
		Crawlerfilestatus comparedCrawlerfilestatus = (Crawlerfilestatus) obj;
		if (!CheckPropertyEqual(crawlerfilestatus.getName(), comparedCrawlerfilestatus.getName())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

